package cn.jsj.cxsys.service.impl;

import cn.jsj.cxsys.service.*;
import org.springframework.stereotype.*;
import cn.jsj.cxsys.dao.*;
import javax.annotation.*;
import org.springframework.beans.factory.annotation.*;
import cn.jsj.cxsys.pojo.*;
import org.springframework.transaction.annotation.*;
import com.github.pagehelper.*;
import java.util.*;

@Service
public class UserServiceImpl implements UserService
{
    @Resource
    private UserFunction userFunction;
    @Value("${superinformation.account}")
    private String superAccount;
    @Value("${superinformation.password}")
    private String superPassword;

    @Transactional
    @Override
    public String RegisterStudent(final User user) {
        if (user == null || user.getAccount() == null || user.getAccount().trim().length() < 6 || user.getAccount().trim().length() > 16 || user.getName() == null || user.getName().equals("") || user.getPassword() == null || user.getPassword().length() > 20 || user.getPassword().length() < 6 || user.getTeleNumber() == null || user.getTeleNumber().length() != 11 || user.getTeleNumber().charAt(0) != '1' || user.getClassName() == null || user.getClassName().equals("") || user.getName().length() > 16) {
            return "paramLess";
        }
        final boolean k2 = LetterNumberJudgeMent.isLetterOfNumber(user.getAccount());
        if (!k2) {
            return "paramLess";
        }
        int t = this.userFunction.haveAccount(user.getAccount().trim());
        if (t != 0) {
            return "accountExist";
        }
        t = this.userFunction.haveTeleNumber(user.getTeleNumber());
        if (t != 0) {
            return "telephoneNumberError";
        }
        t = this.userFunction.haveClassName(user.getClassName().trim());
        if (t != 1) {
            return "classNotExist";
        }
        t = this.userFunction.insertOneStudent(user);
        if (t == 1) {
            return "success";
        }
        return "error";
    }

    @Transactional
    @Override
    public String RegisterTeacher(final User user) {
        if (user == null) {
            return "error";
        }
        user.setClassName("\u8ba1\u7b97\u673a\u5b66\u9662\u6559\u804c\u5de5");
        final String inf = this.RegisterStudent(user);
        if (!inf.equals("success")) {
            return inf;
        }
        final int i = this.userFunction.setUserToTeacher(user.getAccount());
        if (i == 0) {
            this.userFunction.deleteOneUserByAccount(user.getAccount());
            return "error";
        }
        return "success";
    }

    @Override
    public String login(final User user) {
        if (user == null || user.getAccount() == null || user.getPassword() == null || user.getAccount().length() < 5 || user.getAccount().length() > 12 || user.getPassword().length() > 20) {
            return "error";
        }
        int t = this.userFunction.judgeUserPojo(user);
        if (t == 0) {
            return "error";
        }
        t = this.userFunction.isTeacher(user.getAccount());
        if (t == 1) {
            return "teacher";
        }
        return "student";
    }

    @Override
    public String login(final String account, final String password) {
        if (account == null || password == null || account.equals("") || password.equals("")) {
            return "error";
        }
        if (this.superAccount.equals(account) && this.superPassword.equals(password)) {
            return "superLader";
        }
        int t = this.userFunction.judgeLogin(account, password);
        if (t == 0) {
            return "error";
        }
        t = this.userFunction.isTeacher(account);
        if (t == 1) {
            return "teacher";
        }
        return "student";
    }

    @Override
    public List<String> getAllClassName() {
        return this.userFunction.getClasses();
    }

    @Override
    public String updateUserInformation(final User user) {
        if (user == null || user.getPassword() == null || user.getPassword().length() < 5 || user.getPassword().length() > 20 || user.getClassName() == null || user.getClassName().length() < 2 || user.getTeleNumber() == null || user.getTeleNumber().length() != 11 || user.getName() == null || user.getName().length() > 16 || user.getName().length() < 2) {
            return "paramLess";
        }
        final String inf = this.login(user);
        if (!inf.equals("success")) {
            return "accountError";
        }
        int i = this.userFunction.haveClassName(user.getClassName());
        if (i == 0) {
            return "paramLess";
        }
        i = this.userFunction.updateInformation(user);
        if (i == 1) {
            return "success";
        }
        return "error";
    }

    @Override
    public User getUserByAccount(final String account, final String password) {
        if (account == null || account.equals("") || password == null || password.equals("")) {
            return null;
        }
        return this.userFunction.getUserByAccount(account, password);
    }

    @Override
    public List<User> findAllUsers(final String account, final String password, final int pageNum, final int pageSize) {
        final String inf = this.login(account, password);
        if ("superLader".equals(inf)) {
            PageHelper.startPage(pageNum, pageSize);
            return this.userFunction.getAllUser();
        }
        return null;
    }

    @Override
    public String updateUserInformation(final String superAccount, final String password, final List<User> user) {
        if (user == null || user.size() == 0) {
            return "error";
        }
        for (final User i : user) {
            if (i.getAccount() == null || i.getAccount().trim().equals("") || i.getUserID() < 1) {
                return "error";
            }
        }
        final String inf = this.login(superAccount, password);
        if ("superLader".equals(inf)) {
            final StringBuffer result = new StringBuffer("");
            for (final User j : user) {
                final int k = this.userFunction.updateInformationByLader(j);
                if (k == 0) {
                    result.append(j.getAccount().trim() + ",");
                }
            }
            if (result.length() != 0) {
                result.deleteCharAt(result.length() - 1);
                result.append("\u8fde\u81ea\u5df1\u7684\u4e2a\u4eba\u4fe1\u606f\u90fd\u5199\u4e0d\u5bf9");
            }
            else {
                result.append("\u5168\u90e8\u4fee\u6539\u6210\u529f");
            }
            return result.toString();
        }
        return "error";
    }

    @Override
    public String checkStudentState(final User user) {
        final String inf = this.login(user);
        if ("error".equals(inf) || "superLader".equals(inf)) {
            return "error";
        }
        int i = this.userFunction.checkUserIsSigned(user.getUserID());
        if (i == 1) {
            return "inLab";
        }
        i = this.userFunction.checkUserIsSignedComputer(user.getUserID());
        if (i == 1) {
            return "onComputer";
        }
        return "outLine";
    }

    @Override
    public int getUserNumber() {
        return this.userFunction.countNumberOfUsers();
    }

    @Override
    public int countTeachers() {
        return this.userFunction.countNumberOfTeachers();
    }

    @Override
    public String changePassword(final String account, final String password, final String newPassword) {
        if (newPassword == null || newPassword.trim().length() < 5 || newPassword.trim().length() > 20) {
            return "paramLess";
        }
        final int i = this.userFunction.userChangePassword(account, password, newPassword.trim());
        if (i == 1) {
            return "success";
        }
        return "error";
    }

    @Override
    public List<User> superGetAllTeacher(final String superAccount, final String password, final int pageNum, final int pageSize) {
        final String inf = this.login(superAccount, password);
        if ("superLader".equals(inf)) {
            PageHelper.startPage(pageNum, pageSize);
            return this.userFunction.getAllteachers();
        }
        return null;
    }

    @Override
    public List<User> searchUsers(final String superAccount, final String password, final int userID, final String account, final String className, final String name, final String teleNumber, final int pageNum, final int pageSize) {
        final String inf = this.login(superAccount, password);
        if ("superLader".equals(inf)) {
            PageHelper.startPage(pageNum, pageSize);
            return this.userFunction.searchUsers(userID, account, className, name, teleNumber);
        }
        return null;
    }
}
